package com.example.administrator.myrxjavademo.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.administrator.myrxjavademo.R;
import com.example.administrator.myrxjavademo.net.HttpResultTransformer;
import com.example.administrator.myrxjavademo.net.NetClient;
import com.example.administrator.myrxjavademo.net.ProgressSubscriber;
import com.example.administrator.myrxjavademo.net.api.ApiService;
import com.example.administrator.myrxjavademo.net.model.HttpResult;
import com.example.administrator.myrxjavademo.net.model.enity.BannerResponse;
import com.example.administrator.myrxjavademo.net.model.enity.UserPavilionResponse;

import java.util.ArrayList;
import java.util.HashMap;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    private Button button;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();


    }

    private void initView() {
        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(this);
        button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(this);
        button5 = (Button) findViewById(R.id.button5);
        button5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                goAction1();
                break;
            case R.id.button2:
                goAction2();
                break;
            case R.id.button3:
                break;
            case R.id.button4:
                break;
            case R.id.button5:
                break;
        }
    }

    /**
     * 获取馆
     */
    private void goAction2() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", 36);
        map.put("lng", 120.5);
        map.put("lat", 46.5);
        NetClient.getInstance().createApi(ApiService.class).getPavilion(map)
                .compose(new HttpResultTransformer<UserPavilionResponse>())
                .subscribe(new ProgressSubscriber<UserPavilionResponse>() {
                    @Override
                    public void onNext(UserPavilionResponse userPavilionResponse) {
                        Log.i(TAG, "onNext: >> " + userPavilionResponse.toString());
                    }
                });
    }

    private void goAction1() {

//        Observable<ArrayList<BannerResponse>> compose = NetClient.getInstance().createApi().getBanner("24")
//                .compose(new HttpResultTransformer<ArrayList<BannerResponse>>());

        NetClient.getInstance().createApi(ApiService.class).getBanner("24")
                .compose(new HttpResultTransformer<ArrayList<BannerResponse>>())

                /////////////////////////////// 开始
                // 瞎胡闹着处理的
                .flatMap(new Func1<ArrayList<BannerResponse>, Observable<BannerResponse>>() {

                    @Override
                    public Observable<BannerResponse> call(ArrayList<BannerResponse> bannerResponses) {
                        return Observable.from(bannerResponses);
                    }
                })
                .map(new Func1<BannerResponse, String>() {
                    @Override
                    public String call(BannerResponse bannerResponse) {
                        return bannerResponse.toString();
                    }
                })

                ///////////////////////  结束
                .subscribe(new ProgressSubscriber<String>(this) {
                    @Override
                    public void onNext(String s) {
                        Log.i(TAG, "onNext: 带劲>>  " + s);
                    }
                });


    }

    private void one(Subscriber<String> subscriber) {
        NetClient.getInstance().createApi(ApiService.class).getBanner("10")
                .map(new Func1<HttpResult<ArrayList<BannerResponse>>, ArrayList<BannerResponse>>() {
                    @Override
                    public ArrayList<BannerResponse> call(HttpResult<ArrayList<BannerResponse>> arrayListHttpResult) {
                        if ("0".equals(arrayListHttpResult.ret)) {
                            return arrayListHttpResult.data;
                        } else {
                            // TODO: 2017\8\17 0017  抛出异常
                            return null;
                        }
                    }
                })
                .flatMap(new Func1<ArrayList<BannerResponse>, Observable<BannerResponse>>() {
                    @Override
                    public Observable<BannerResponse> call(ArrayList<BannerResponse> bannerResponses) {
                        return Observable.from(bannerResponses);

                    }
                })
                .map(new Func1<BannerResponse, String>() {
                    @Override
                    public String call(BannerResponse bannerResponse) {
                        return bannerResponse.toString();
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
